<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <!-- 左侧 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧 -->
      <ul class="right">
        <li>
          <img
            :src="userInfo.headImg"
            class="avatar"
            alt=""
          />&nbsp;用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网{{ isLogined }}</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="loginbtn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { WeixinLoginApi, UserProfilesAPI } from "@/request/api";
export default {
  created() {
    // console.log(this.$route.query);
    // console.log("*router-created", this.$route.query.code);
    this.$router.onReady(async () => {
      let pathCode = this.$route.query.code;
      if (pathCode) {
        let res = await WeixinLoginApi({
          code: pathCode,
        });
        // console.log(res);
        if (!res) return;
        // console.log("1111", res);
        if (res.code == 0) {
          //扫码登录成功
          // 1.提示扫码登录成功.
          this.asyncShowToast({
            isShow: true,
            msg: "提示扫码登录成功",
            type: "success",
          });
          // 2.存储token x-auth-token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          // 3.切换按钮（显示购物车按钮）
          this.changeIsLogined(true);
          // 4.清除地址栏中的code（避免重复发送微信扫码登录请求）
          this.$router.push(this.$route.path);
          // 5.加载用户信息
          this.asyncChangeUserInfo();
        } else if (res.code == 400) {
          //二维码超时2Min   code已经使用过了，再次发送
          // 1.提示二维码失效了，请重新扫码
          this.asyncShowToast({
            isShow: true,
            msg: "二维码失效了，请重新扫码",
            type: "warning",
          });
          // 2.自动打开模态框
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          //用户的微信还没有在这个网站上绑定过手机
          // 1.提示绑定
          this.asyncShowToast({
            isShow: true,
            msg: "请使用手机号绑定微信登录",
            type: "warning",
          });
          // 2.自动打开模态框
          this.changeIsShowLoginModal(true);
          // 3.本地存储uuid(下次手机号登录时使用)
          localStorage.setItem("uuid", res.uuid);
          // console.log(res.uuid);
          // 4.清除地址栏中的code (避免重复发送微信扫码登录)
          console.log(this.$route.path);
          this.$router.push(this.$route.path);
        }
      } else {
        // console.log("进入了created中的else代码");
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      changeUserInfo: "userInfo/changeUserInfo",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
};
</script>

<style lang="less" scored>
.topbar {
  width: 100%;
  line-height: 40px;
  height: 40px;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  background-color: #242b39;
  color: #fff;
}
.topbarwrap {
  display: flex;
  justify-content: space-between;
  .right {
    li {
      float: left;
      margin-left: 20px;
      color: #fffefe;
      cursor: pointer;
      text-align: center;
      &.loginbtn {
        background-color: #0a328e;
        width: 124px;
      }
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      .avatar {
        width: 23px;
        height: 22px;
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>